package com.cskaoyan.market.service;


import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;


import java.time.LocalDateTime;
import java.util.*;

public class MarketConfigServiceImpl implements MarketConfigService {

    @Override
    public Map<String, String> express() {
        Map<String, String> result = new HashMap<>();
        SqlSession session = MybatisUtils.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<String> list = new ArrayList<>();
        list.add("market_express_freight_value");
        list.add("market_express_freight_min");
        criteria.andKeyNameIn(list);
        List<MarketSystem> marketSystems = mapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        for (MarketSystem marketSystem : marketSystems) {
            result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        }
        session.commit();
        session.close();
        return result;
    }

    @Override
    public Map<String, String> order() {
        Map<String, String> result = new HashMap<>();
        SqlSession session = MybatisUtils.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<String> list = new ArrayList<>();
        list.add("market_order_comment");
        list.add("market_order_unconfirm");
        list.add("market_order_unpaid");
        criteria.andKeyNameIn(list);
        List<MarketSystem> marketSystems = mapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        for (MarketSystem marketSystem : marketSystems) {
            result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        }
        session.commit();
        session.close();
        return result;
    }

    @Override
    public Map<String, String> wx() {
        Map<String, String> result = new HashMap<>();
        SqlSession session = MybatisUtils.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<String> list = new ArrayList<>();
        list.add("market_wx_catlog_goods");
        list.add("market_wx_catlog_list");
        list.add("market_wx_index_brand");
        list.add("market_wx_index_hot");
        list.add("market_wx_index_new");
        list.add("market_wx_index_topic");
        list.add("market_wx_share");
        criteria.andKeyNameIn(list);
        List<MarketSystem> marketSystems = mapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        for (MarketSystem marketSystem : marketSystems) {
            result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        }
        session.commit();
        session.close();
        return result;
    }

    @Override
    public Map<String, String> mall() {
        Map<String, String> result = new HashMap<>();
        SqlSession session = MybatisUtils.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<String> list = new ArrayList<>();
        list.add("market_mall_address");
        list.add("market_mall_latitude");
        list.add("market_mall_longitude");
        list.add("market_mall_name");
        list.add("market_mall_phone");
        list.add("market_mall_qq");
        criteria.andKeyNameIn(list);
        List<MarketSystem> marketSystems = mapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        for (MarketSystem marketSystem : marketSystems) {
            result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        }
        session.commit();
        session.close();
        return result;
    }

    @Override
    public void update(Map<String, Object> map) {
        SqlSession session = MybatisUtils.getSession();
        MarketSystemMapper mapper = session.getMapper(MarketSystemMapper.class);
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        try {
            for (Map.Entry<String, Object> entry : entries) {
                MarketSystemExample marketSystemExample = new MarketSystemExample();
                MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
                criteria.andKeyNameEqualTo(entry.getKey());
                MarketSystem marketSystem = mapper.selectOneByExampleSelective(marketSystemExample);
                marketSystem.setKeyValue(entry.getValue().toString());
                marketSystem.setUpdateTime(LocalDateTime.now());
                mapper.updateByExampleSelective(marketSystem, marketSystemExample);
            }
            session.commit();
        } catch (Exception e) {
            session.rollback();
            throw new RuntimeException(e);
        } finally {
            session.close();
        }
    }
}

